package com.lingxu.module.zx.controller;


import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.system.manage.entity.SysUser;
import com.lingxu.base.system.manage.service.ISysUserService;
import com.lingxu.module.zx.mapper.StatisticsMapper;
import com.lingxu.module.zx.util.CheckVariable;
import com.lingxu.module.zx.util.DrUtils;
import com.lingxu.module.zx.vo.YcsjForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/zx/tj")
@Api(tags = "专项统计")
public class StatisticsController {


    @Autowired
    private StatisticsMapper statisticsMapper;

    @Autowired
    private ISysUserService userService;



    @GetMapping("getSjly")
    @ApiOperation("数据源统计")
    public Result getSjly(String modelId){

        if(CheckVariable.isEmpty(modelId)){
            return Result.error("参数异常!");
        }
        List<String> sjly = statisticsMapper.getSjly(modelId);



        Map<String,Integer> res = new HashMap<>();
        if(!CheckVariable.isEmpty(sjly)){
            List<String> tmpList = new ArrayList<>();
            sjly.forEach(s->{
                 String[] split = s.split(";");
                for (String ss :split) {
                    tmpList.add(ss);
                }
            });


            Map<String, Long> collect = tmpList.stream().collect(Collectors.groupingBy(v -> v, Collectors.counting()));
            collect.forEach((k,v)->{
//                BigDecimal divide = BigDecimal.valueOf(v).divide(total,3,RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).setScale(0,RoundingMode.HALF_UP);
                res.put(k,v.intValue());
            });
        }
        List<Map<String,Object>> list = new ArrayList<>();
        res.forEach((k,v)->{
            Map<String,Object> tmp  = new HashMap<>();
            tmp.put("name",k);
            tmp.put("val",v);
            list.add(tmp);
        });
        return Result.ok(list);
    }


    @GetMapping("getYdwtlxfb")
    @ApiOperation("疑点问题类型分布")
    public Result getYdwtlxfb(String modelId){

        if(CheckVariable.isEmpty(modelId)){
            return Result.error("参数异常!");
        }
        //先查该模型的所有子模型id
        String linkModelId = statisticsMapper.getLinkModelId(modelId);

        if(CheckVariable.isEmpty(linkModelId)){
            return Result.ok(new ArrayList<>());
        }

        List<String> arrayList = Arrays.asList(linkModelId.split(","));

        List<Map<String, Object>> ydwtlxfb = statisticsMapper.getYdwtlxfb(arrayList);

        return Result.ok(ydwtlxfb);
    }


    @GetMapping("getWtdqfb")
    @ApiOperation("问题地区分布")
    public Result getWtdqfb(String modelId,String userId){

        if(CheckVariable.isEmpty(modelId) || CheckVariable.isEmpty(userId)){
            return Result.error("参数异常!");
        }

        //获取当前用户是所属地区
        SysUser su = userService.getById(userId);

        String area = statisticsMapper.getArea(su.getRegion());
        if(CheckVariable.isEmpty(area)){
            return Result.error("问题地区分布查询异常");
        }
         String suff = area.substring(area.length() - 1);
        String pre = area.substring(0, 1);
        List<Map<String, Object>> areas = new ArrayList<>();
        if("省".equals(suff)){
            //统计该省下面所有的市的信息
            areas = statisticsMapper.getChildrenAreaById(su.getRegion());
        }else if("市".equals(suff)){
           areas = statisticsMapper.getChildrenAreaById(su.getRegion());
        }else if("区".equals(suff)){
            areas = statisticsMapper.getChildrenAreaByPId(su.getRegion());
        }else if("县".equals(suff)){
            areas = statisticsMapper.getChildrenAreaByPId(su.getRegion());
        }else{
            if("省".equals(pre)){
                //统计该省下面所有的市的信息
                areas = statisticsMapper.getChildrenAreaByPId(su.getRegion());
            }else if("市".equals(pre)){
                areas = statisticsMapper.getChildrenAreaByPId(su.getRegion());
            }else if("区".equals(pre)){
                areas = statisticsMapper.getChildrenAreaByPId(su.getRegion());
            }else if("县".equals(pre)){
                areas = statisticsMapper.getChildrenAreaByPId(su.getRegion());
            }else{
                return Result.error("问题地区分布查询异常");
            }

        }
        //先查该模型的所有子模型id
        String linkModelId = statisticsMapper.getLinkModelId(modelId);

        List<Map<String, Object>> ydwtlxfb = new ArrayList<>();
        if(!CheckVariable.isEmpty(linkModelId)){
            List<String> arrayList = Arrays.asList(linkModelId.split(","));
            List<Integer> aids = areas.stream().map(v -> Integer.valueOf(v.get("ID").toString())).collect(Collectors.toList());
            ydwtlxfb = statisticsMapper.getWtdqfb(arrayList,aids);
        }
         Map<String, Map<String, Object>> cname = ydwtlxfb.stream().collect(Collectors.toMap(v -> v.get("CNAME").toString(), x -> x));
         List<Map<String,Object>> res = new ArrayList<>();
        areas.forEach(a->{
            Map<String,Object> tmp = new HashMap<>();
            String cname1 = a.get("CNAME").toString();
            Map<String, Object> map = cname.get(cname1);
            tmp.put("id",a.get("ID"));
            tmp.put("pid",a.get("PID"));
            if(CheckVariable.isEmpty(map)){
                tmp.put("name",cname1);
                tmp.put("val",0);
            }else{
                tmp.put("name",cname1);
                tmp.put("val",map.get("TOTAL"));
            }
            res.add(tmp);
        });


        if("省".equals(suff)){
            return sum(su, res);

        }
        if("省".equals(pre)){
            return sum(su, res);
        }
        return Result.ok(res);
    }

    private Result sum(SysUser su, List<Map<String, Object>> res) {
        //获取省信息
        Map<String, Object> s = statisticsMapper.getS(su.getRegion());
//统计该省下面所有的市的信息
        List<Map<String, Object>> collect = res.stream().filter(v -> v.get("pid").toString().equals(s.get("ID").toString())).collect(Collectors.toList());
        collect.forEach(j->{
            Integer sum = res.stream().filter(v -> j.get("id").toString().equals(v.get("pid").toString())).mapToInt(x -> Integer.valueOf(x.get("val").toString())).sum();
            j.put("val",Integer.parseInt(j.get("val").toString())+sum);
        });

        return Result.ok(collect);
    }


    @GetMapping("getSjtj")
    @ApiOperation("数据统计")
    public Result getSjtj(String modelId){

        if(CheckVariable.isEmpty(modelId)){
            return Result.error("参数异常!");
        }
        //先查该模型的所有子模型id
        String linkModelId = statisticsMapper.getLinkModelId(modelId);

        if(CheckVariable.isEmpty(linkModelId)){
            return Result.ok(new ArrayList<>());
        }

        List<String> arrayList = Arrays.asList(linkModelId.split(","));

        //查询数据总量
        Integer sjzl = statisticsMapper.getSjzl(arrayList);

        //查询模型规则
        Integer mxgz = statisticsMapper.getMxgz(modelId);

        //查询模型背景资料 todo 暂无
        Integer bjzl =0;

        //查询发现异常数据
        Integer fxycsj = statisticsMapper.getYcsj(arrayList);

        //查看核查数据
        Integer hcsj = statisticsMapper.getHcsj(arrayList);

        //查询处置人员
        Integer czry = statisticsMapper.getCzry(modelId);

        //查询模型典型案例 todo 暂无
        Integer dxal =0;

        //查询模型建章立制 todo 暂无
        Integer jzlz =0;

        Map<String,Object> res = new HashMap<>();
        res.put("sjzl",sjzl==null?0:sjzl);
        res.put("mxgz",mxgz==null?0:mxgz);
        res.put("bjzl",bjzl==null?0:bjzl);
        res.put("fxycsj",fxycsj==null?0:fxycsj);
        res.put("hcsj",hcsj==null?0:hcsj);
        res.put("czry",czry==null?0:czry);
        res.put("dxal",dxal==null?0:dxal);
        res.put("jzlz",jzlz==null?0:jzlz);
        return Result.ok(res);
    }




    @GetMapping("getKydx")
    @ApiOperation("可疑对象")
    public Result getKydx(String modelId){

        if(CheckVariable.isEmpty(modelId)){
            return Result.error("参数异常!");
        }
        //先查该模型的所有子模型id
        String linkModelId = statisticsMapper.getLinkModelId(modelId);

        if(CheckVariable.isEmpty(linkModelId)){
            return Result.ok(new ArrayList<>());
        }

        List<String> arrayList = Arrays.asList(linkModelId.split(","));

         List<Map<String, Object>> kydx = statisticsMapper.getKydx(arrayList);

        return Result.ok(kydx);
    }



    @GetMapping("getCcajfb")
    @ApiOperation("查处案件分布")
    public Result getCcajfb(String modelId){

        if(CheckVariable.isEmpty(modelId)){
            return Result.error("参数异常!");
        }

        List<Map<String, Object>> ydwtlxfb = statisticsMapper.getCcajfb(modelId);
        if(!CheckVariable.isEmpty(ydwtlxfb)){
            ydwtlxfb.forEach(v->{
                v.put("drName", DrUtils.drName.get(v.get("DR").toString()));
            });

        }
        return Result.ok(ydwtlxfb);
    }



    @PostMapping("getYcsjList")
    @ApiOperation("查询异常数据运行列表")
    public Result getYcsjList(@RequestBody YcsjForm ycsjForm){

        if(CheckVariable.isEmpty(ycsjForm.getModelId())){
            return Result.error("参数异常!");
        }

        if(CheckVariable.isEmpty(ycsjForm.getPage())){
            ycsjForm.setPage(1);
        }
        if(CheckVariable.isEmpty(ycsjForm.getPageSize())){
            ycsjForm.setPageSize(10);
        }

        ycsjForm.setPage((ycsjForm.getPage()-1)*ycsjForm.getPageSize());


        Integer count = statisticsMapper.getYcsjListCount(ycsjForm);

        List<Map<String, Object>> ydwtlxfb = statisticsMapper.getYcsjList(ycsjForm);

        if(!CheckVariable.isEmpty(ydwtlxfb)){
            ydwtlxfb.forEach(v->{
                v.put("STATUSNAME",DrUtils.runStatusName.get(v.get("RUNSTATUS")));
            });
        }

        Map<String,Object> res =new HashMap<>();
        res.put("total",count);
        res.put("data",ydwtlxfb);
        return Result.ok(res);
    }


    @PostMapping("getChildrenModel")
    @ApiOperation("根据主模型id查询所有子模型")
    public Result getChildrenModel(String modelId){

        if(CheckVariable.isEmpty(modelId)){
            return Result.error("参数异常!");
        }
        List<Map<String, Object>> childrenModel = statisticsMapper.getChildrenModel(modelId);

        return Result.ok(childrenModel);
    }



}
